package gin_http

import (
	"gitee.com/captials-team/ubdframe/src/common/consts"
	"gitee.com/captials-team/ubdframe/src/domain/dto/respdata"
	"gitee.com/captials-team/ubdframe/src/pkg/i18n"
	"github.com/gin-gonic/gin"
	"net/http"
)

// I18nController i18n国际化Controller
type I18nController struct {
	Parser       i18n.II18nParser
	ErrIgnoreLog bool //错误返回是否忽略日志
}

func (ctr *I18nController) Response(ctx *gin.Context, data respdata.ResponseData, msgData ...map[string]string) {
	ctr.ResponseCode(ctx, http.StatusOK, data, msgData...)
}

func (ctr *I18nController) ResponseCode(ctx *gin.Context, code int, data respdata.ResponseData, msgData ...map[string]string) {
	parser := ctr.Parser.Lang(ctx.GetHeader(consts.AcceptLanguageKey))
	if ctr.ErrIgnoreLog && data.Code > 0 {
		//支持操作日志log-不记录处理
		ctx.Set(consts.OperateLogContextKey, "")
	}
	ctx.JSON(code, data.MMsg(parser.ParseMsg(data.Msg, msgData...)))
}
